//
//  ANChangeCountView.m
//  Ants
//
//  Created by ZHM on 16/8/30.
//  Copyright © 2016年 ZHM. All rights reserved.
//

#import "ANChangeCountView.h"

@implementation ANChangeCountView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.userInteractionEnabled = YES;
    }
    return self;
}
- (void)setChooseCount:(NSInteger)choosedCount totalCount:(NSInteger)totalCount {
    self.choosedCount = choosedCount;
    self.totalCount = totalCount;
    [self setUpViews];
}

- (UIButton *)subButton {
    if (!_subButton) {
        _subButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _subButton.frame = CGRectMake(0, 0, [UIImage imageNamed:@"product_detail_sub_normal"].size.width, [UIImage imageNamed:@"product_detail_sub_normal"].size.height);
        [_subButton setBackgroundImage:[UIImage imageNamed:@"product_detail_sub_normal"] forState:UIControlStateNormal];
        [_subButton setBackgroundImage:[UIImage imageNamed:@"product_detail_sub_no"] forState:UIControlStateDisabled];
        _subButton.backgroundColor = [UIColor clearColor];
        _subButton.exclusiveTouch = YES;
        [self addSubview:_subButton];
        if (self.choosedCount <= 1) {
            _subButton.enabled = NO;
        }else{
            _subButton.enabled = YES;
        }
    }
    return _subButton;
}
- (UITextField *)numberTF {
    if (!_numberTF) {
        _numberTF = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_subButton.frame), 0, 40, _subButton.frame.size.height)];
        _numberTF.textAlignment = NSTextAlignmentCenter;
        _numberTF.keyboardType = UIKeyboardTypeNumberPad;
        _numberTF.clipsToBounds = YES;
        _numberTF.layer.borderColor = [[UIColor lightGrayColor] CGColor];
        _numberTF.layer.borderWidth = 0.5;
        _numberTF.textColor = [UIColor blackColor];
        _numberTF.font=[UIFont systemFontOfSize:13];
        _numberTF.backgroundColor = [UIColor whiteColor];
        [self addSubview:_numberTF];
    }
    return _numberTF;
}
- (UIButton *)addButton {
    if (!_addButton) {
        _addButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _addButton.frame = CGRectMake(CGRectGetMaxX(_numberTF.frame), 0, [UIImage imageNamed:@"product_detail_sub_normal"].size.width, [UIImage imageNamed:@"product_detail_sub_normal"].size.height);
        _addButton.backgroundColor=[UIColor clearColor];
        
        [_addButton setBackgroundImage:[UIImage imageNamed:@"product_detail_add_normal"] forState:UIControlStateNormal];
        [_addButton setBackgroundImage:[UIImage imageNamed:@"product_detail_add_no"] forState:UIControlStateDisabled];
        
        _addButton.exclusiveTouch = YES;
        [self addSubview:_addButton];
        if (self.choosedCount >= self.totalCount) {
            _addButton.enabled = NO;
        }else{
            _addButton.enabled = YES;
        }
    }
    return _addButton;
}
- (void)setUpViews
{
    if (self.choosedCount <= 1) {
        self.subButton.enabled = NO;
    }else{
        self.subButton.enabled = YES;
    }
    self.numberTF.text = [NSString stringWithFormat:@"%ld",self.choosedCount];
    if (self.choosedCount >= self.totalCount) {
        self.addButton.enabled = NO;
    }else{
        self.addButton.enabled = YES;
    }
}


@end
